<!-- src/pages/user/UserRegisterPage.vue -->
<template>
  <div class="register-container">
    <!-- 左侧品牌区 -->
    <div class="brand-panel">
      <img src="@/assets/logo.png" alt="logo" class="logo" />
      <h1 class="brand-title">潮汐智能云图库</h1>
      <p class="brand-desc">企业级智能协同 · 安全 · 高效</p>
    </div>

    <!-- 右侧注册区 -->
    <div class="form-panel">
      <div class="form-card">
        <h2 class="form-title">创建账户</h2>
        <div class="form-desc">注册后开始管理您的图片资产</div>

        <a-form
          :model="formState"
          name="register"
          autocomplete="off"
          @finish="handleSubmit"
          @finishFailed="handleError"
        >
          <a-form-item
            name="userAccount"
            :rules="[
              { required: true, message: '请输入账号' },
              { min: 4, message: '账号不少于 4 位' }
            ]"
          >
            <a-input
              v-model:value="formState.userAccount"
              size="large"
              placeholder="账号"
            >
              <template #prefix>
                <UserOutlined />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item
            name="userPassword"
            :rules="[
              { required: true, message: '请输入密码' },
              { min: 8, message: '密码不少于 8 位' }
            ]"
          >
            <a-input-password
              v-model:value="formState.userPassword"
              size="large"
              placeholder="密码"
            >
              <template #prefix>
                <LockOutlined />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item
            name="checkPassword"
            :rules="[
              { required: true, message: '请确认密码' },
              { validator: validateConfirmPassword }
            ]"
          >
            <a-input-password
              v-model:value="formState.checkPassword"
              size="large"
              placeholder="确认密码"
            >
              <template #prefix>
                <LockOutlined />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item>
            <a-button
              type="primary"
              html-type="submit"
              size="large"
              :loading="loading"
              block
            >
              注册
            </a-button>
          </a-form-item>

          <div class="tips">
            已有账户？
            <RouterLink to="/user/login">立即登录</RouterLink>
          </div>
        </a-form>
      </div>

      <!-- 页脚 -->
      <GlobalFooter class="inner-footer" theme="light" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import GlobalFooter from '@/components/GlobalFooter.vue'
import { userRegisterUsingPost } from '@/api/userController'

const router = useRouter()

interface FormState {
  userAccount: string
  userPassword: string
  checkPassword: string
}

const formState = reactive<FormState>({
  userAccount: '',
  userPassword: '',
  checkPassword: ''
})

const loading = ref(false)

// 验证确认密码
const validateConfirmPassword = (_rule: any, value: string) => {
  if (!value) {
    return Promise.reject('请确认密码')
  }
  if (value !== formState.userPassword) {
    return Promise.reject('两次输入的密码不一致')
  }
  return Promise.resolve()
}

const handleSubmit = async () => {
  loading.value = true
  try {
    const res = await userRegisterUsingPost({
      userAccount: formState.userAccount,
      userPassword: formState.userPassword,
      checkPassword: formState.checkPassword
    })

    if (res.data.code === 0) {
      message.success('注册成功')
      // 注册成功后跳转到登录页面
      router.push('/user/login')
    } else {
      message.error(res.data.message || '注册失败')
    }
  } catch (error) {
    message.error('注册失败')
  } finally {
    loading.value = false
  }
}

const handleError = () => message.warning('请检查输入')
</script>

<style scoped>
.register-container {
  display: flex;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

/* 品牌面板 */
.brand-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px;
}
.logo {
  width: 80px;
  margin-bottom: 16px;
}
.brand-title {
  font-size: 32px;
  margin: 0 0 8px;
}
.brand-desc {
  font-size: 16px;
  opacity: 0.9;
}

/* 表单面板 */
.form-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
}
.form-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
}
.form-title {
  margin: 0 0 8px;
  font-size: 24px;
}
.form-desc {
  color: #999;
  margin-bottom: 32px;
}
.tips {
  text-align: center;
  font-size: 13px;
  color: #666;
}
.inner-footer {
  margin-top: 40px;
  background: transparent !important;
  box-shadow: none !important;
}

/* 响应式 */
@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
  }
  .brand-panel {
    display: none;
  }
  .form-panel {
    padding: 24px;
  }
}
</style>
